<template>
	<view class="create_customer">

		<view class="fllow_form" style="margin: 30rpx 0;">
			<input type="text" v-model="memberName"  placeholder-style="font-size:30rpx;color: #999;" placeholder="输入客户名称"/>
			<view class="form_right theme">
				<image style="width: 40rpx;height: 40rpx;
				margin: 0 20rpx 0 30rpx;" :src="BASE_IMG_URL+'chachong.png'" mode="scaleToFill"></image>
				<view @click="checkName" style="color:#03AA96;">查重</view>
			</view>
		</view>  
		<view class="set_head">
			<view class="set_img">
				<image :src="photo_url ? photo_url : BASE_IMG_URL+'headImg.png'"  mode="scaleToFill"></image>
			</view>
			<view class="set_name" @click="changeHeaderImg">
				<view>修改头像</view>
				<view class="iconfont icon-arrows_right"></view>
			</view>
		</view>  
		<form-item v-for="(item,index) in listForm" :key="index" :objInfo="item" @changeData="changeData"></form-item>
		<view id="btn" class="btn_g bg-blue button-hover round" @click="subSave">
			保存
		</view>
		 
	</view>
</template>

<script>
	import formItem from '@/components/form/formitem.vue'
	import {pageJumps} from "./createCust_page_jumps.js";
	import { netSelect, netPrevCustomer, netCheckName, netKehuDetail, netEditKehu } from '@/api/kehu.js' 
	import { netSetForm } from '@/api/index.js'
	import { checkPhone } from '@/util/util.js'
	import {
		BASE_IMG_URL,
		BASE_URL,
		SIGN_NO
	} from '@/util/api.js' 
	const citysJSON = require('@/util/address.js');
	export default {
		mixins:[pageJumps],
		components:{
			formItem
		},
		data() {
			return {
				listForm: [],
				BASE_IMG_URL: BASE_IMG_URL,
				noClick: true, //防止  重复点击
				 
				age: '',
				carId: '',
				career: '',
				source: '1',
				address: '',
				remark: '',
				id: '',
				requirement: '',
				noSaleReason: '',
				married: '1',
				birthday: '',
				birthdayType: '0',
				telephone: '',
				telephone1: '',
				category: '',
				saleStatus_category: '',
				triage_status: '',
				photo_id: '',
				photo_url: null,
				cardnu: '',
				memberName: '',
				mobile: '',
				endTime: '',
				dis_parent1_id: '',
				adviser_ids: '',
				operator_ids: '',
				grade_id: '',
				sex: '0',
				 
			}
		},
		onLoad(options) {
			this.addressList = citysJSON.citys
			this.id = options.id
			//获取  客户 信息
			this.getKehuInfo()
			
			this.getSelectlist()
			
		},
		onShow() {
			
		},
		onHide() {
			
		},
		methods: {
			 
			 changeHeaderImg() {
			 	uni.chooseImage({
			 		count:1,
			 		success:(res)=>{
			 			let file = res.tempFilePaths[0]
			 			this.uploadFile(file)
			 		}
			 	})
			 },
			 uploadFile(file){
			 	uni.uploadFile({
			 		url:BASE_URL+'common/upload',
			 		name:'file',
			 		header:{
			 			'Authori-vipadmin': uni.getStorageSync('token'),
			 		},
			 		formData: {
			 			"uploadPath": "member",
			 		},
			 		// #ifdef MP-ALIPAY
			 		fileType:'image',
			 		// #endif
			 		filePath: file,
			 		success:(res)=>{
			 			console.log("===========")
			 			let data = JSON.parse(res.data).data
			 			if(data.errorCode == 0) { 
			 					this.photo_url = data.retObj[0].webUploadUrl
			 					this.photo_id = data.retObj[0].acc_id
			 				 
			 			}else{
			 				uni.showToast({
			 					title:'上传失败',
			 					icon:'none'
			 				})
			 			} 
			 		},
			 		fail:(err)=>{
			 			console.log(err)
			 		}
			 	})
			 },
			confirm(e) {
				this.address_dingwei = e[0].value+e[1].value+e[2].value
			},
			//获取 客户 信息
			getKehuInfo() {
				netKehuDetail({id:this.id}).then(res=>{
					res = res.data
					this.info = res
					this.memberName = res.memberName
					this.address = res.address
					 this.photo_id= res.photo_id
					 this.photo_url= res.photo_url
			
					//获取附加信息 form
					this.getForm()
				})
			},
			getForm() {
				netSetForm({type:'customer'}).then(res=>{
					res = res.data
					res.forEach(ele=>{
						ele.value = this.info[ele.id]
					})
					this.listForm = res
				})
			},
			handleData(data) {
				let arr = []
				data.forEach(ele=>{
					arr.push(ele.name)
				})
				this.plist = arr
			},
			handleParent() {
				let id = this.parent_id
				this.parentList.forEach((ele,index)=>{
					if(ele.id == id){
						this.parentIndex = index
						this.parentName = ele.name
					}
				})
			},
			changeData(name,value,obj) {
				let arr = this.listForm
				arr.forEach((ele,index)=>{
					if(ele.id == obj.id){
						ele.value = value
						this.$set(this.listForm,index,ele)
					}
				})
			},
			//获取  select
			getSelectlist() {
				netSelect().then(res=>{
						this.followList = res.data['跟进类型']
						this.sourceList = res.data['客户来源']
				})
			},
			//姓名查重
			checkName() {
				let name = this.memberName
				console.log(name)
				if(!name){
					uni.showToast({
					    title: '请填写客户姓名',
						icon:'none',
					    duration: 2000
					})
					return
				}
				netCheckName({name}).then(res=>{
					uni.showToast({
					    title: res.message,
						icon:'none',
					    duration: 2000
					})
					
				})
			},
			
			//位置
			changeLocation(e) {
				this.location = e.detail.value[0]+'/'+e.detail.value[1]+'/'+e.detail.value[2]
			},
			
			//改变上级
			changeParent(e) {
				console.log(e)
				this.parentIndex = e.detail.value
				this.parentName = this.parentList[e.detail.value].name
				this.parent_id = this.parentList[e.detail.value].id
			},
			//是否创建  跟进任务
			change() {
				this.is_event = !this.is_event
			},
			//跟进时间
			changeFollowTime(e) {
				console.log(e)
				this.event_start_time = e.detail.value
			},
			//任务提醒
			changeRemind(e) {
				this.remindIndex = e.detail.value
				this.remindtype = this.remindList[e.detail.value]
			},
			// 详细地址
			detailAddClick() {
				uni.setClipboardData({
				    data: this.info.address_detail,
				    success: () => {
				        uni.showToast({
				        	title:'详细地址已复制，请粘贴搜索',
				        	icon:'none'
				        })
						setTimeout(()=>{
							uni.chooseLocation({
								success: res => {
								     console.log(res)
									 this.lat = res.latitude
									 this.lng = res.longitude
									 this.address_detail = res.name
									 this.address_dingwei = res.address
								},
								fail: err => {
								    console.log(err);
									if(err.errMsg == 'chooseLocation:fail auth deny'){
										uni.showToast({
											title:'请允许使用位置信息',
											icon:'none'
										})
									}
								}
							})
						},2000)
				    }
				})
			},
			//完成
			subSave() {
				let {
					age,
					carId,
					career,
					source,
					address,
					remark,
					kefu_id,
					requirement,
					noSaleReason,
					married,
					birthday,
					birthdayType,
					telephone,
					telephone1,
					category,
					saleStatus_category,
					triage_status,
					photo_id,
					photo_url,
					cardnu,
					memberName,
					mobile,
					endTime,
					dis_parent1_id,
					adviser_ids,
					operator_ids, 
					grade_id,
					sex,
					 
				} = this
				if(!memberName){
					uni.showToast({
					    title: '请填写客户名称',
						icon:'none',
					    duration: 2000
					})
					return
				}
				let params = {}
				let arr = []
				this.listForm.forEach(ele=>{
					arr.push({
						name:ele.id,
						required: ele.config.required,
						msg: ele.config.label,
						value:ele.value
					})
				})
				for(let i = 0; i < arr.length ; i++) {
					if(arr[i].required && !arr[i].value){
						uni.showToast({
							title: arr[i].msg+'不能为空',
							icon:'none'
						})
						return
					}
					if(arr[i].required && arr[i].name == 'mobile' && !checkPhone(arr[i].value)){
						uni.showToast({
							title: '请填写正确的手机号',
							icon:'none'
						})
						return
					}
					if((arr[i].name == 'mobile' || arr[i].name == 'telephone') && arr[i].value && !checkPhone(arr[i].value)){
						uni.showToast({
							title: '请填写正确的手机号',
							icon:'none'
						})
						return
					}
					if(arr[i].required && arr[i].name == 'telephone' && !checkPhone(arr[i].value)){
						uni.showToast({
							title: '请填写正确的手机号',
							icon:'none'
						})
						return
					}
					params[arr[i].name] = arr[i].value
				}
				// params = Object.assign({},params,{id:this.id,name,location,address_detail,lng,lat,parent_id,address:address_dingwei})
				 
				params = Object.assign({}, params, {
					memberName: memberName,
					photo_id: photo_id,
					photo_url: photo_url,
					id:this.id
				})
				   
				console.log(params)
				netEditKehu(params).then(res=>{
					uni.showToast({
					    title: res.message,
						icon:'none',
					    duration: 2000
					})
					setTimeout(()=>{
						uni.navigateBack({
							delta:1
						})
					},2000)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.fllow_form_text{
		background-color: #fff;
		border-bottom: 1rpx solid #EAEAEA;
		padding: 30rpx 40rpx;
		color: #999;
		font-size: 32rpx;
		.theme {
			color: #5ca9fe;
		}
		text {
			color: #f00;
			margin-left: 10rpx;
		}
		.detailText{
			width:85%;
			background:#f8f8f8;
			border-radius: 10rpx;
			height:120rpx;
			padding:10rpx;
			margin:20rpx auto 0;
		}
	}
.create_customer {
	font-size: 28rpx;
	.info_head {
		height: 80rpx;
		line-height: 80rpx;
		font-size: 24rpx;
		color: #999;
		padding-left: 24rpx;
	}
	.mar_top {
		margin-top: 30rpx;
	}
	.fllow_form {
		display: flex;
		justify-content: space-between;
		align-items: center;
		line-height: 40rpx;
		background-color: #fff;
		border-bottom: 1rpx solid #EAEAEA;
		padding: 20rpx 30rpx;
		color:#999;
		font-size:32rpx;
		.form_right {
			display: flex;
			color: #999;
			text-align: right;
			align-items: center;
			image {
				width: 40rpx;
				height: 40rpx;
				margin: 28rpx 5rpx 0 0;
			}
		}
		.theme {
			color: #5ca9fe;
		}
		text {
			color: #f00;
			margin-left: 10rpx;
		}
		.form_input {
			input {
				line-height: 40rpx;
				text-align: right;
				margin-right: 10rpx;
			}
		}
	}
	.fllow_area {
		background-color: #fff;
		padding: 0 30rpx;
		margin-bottom: 30rpx;
		.area_head {
			height: 100rpx;
			line-height: 100rpx;
			text {
				color: #f00;
			}
		}
		textarea {
			width: 100%;
			height: 300rpx;
		}
	}
}
.set_head {
			display: flex;
			justify-content: space-between;
			margin: 30rpx 0;
			padding: 0 24rpx;
			background-color: #fff;
			height: 120rpx;
			line-height: 120rpx;
			border-bottom: 1rpx solid #EAEAEA;
			border-top: 1rpx solid #EAEAEA;

			.set_img {
				image {
					vertical-align: middle;
					width: 80rpx;
					height: 80rpx;
					border-radius: 50%;
				}
			}

			.set_name {
				display: flex;
			}
		}
#btn {
		font-size: 26rpx;
		color: #fff;
		text-align: center;
		line-height: 88rpx;
		width: 500rpx;
		height: 88rpx;
		background: #03AA96;
		border-radius: 29rpx;
		margin: 30rpx auto;
	}
</style>
